﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VS2008Panel;
using System.Windows.Forms.DataVisualization.Charting;
using Core.Base;
using ERP.BusinessEntities.Base;
using System.Windows.Forms;
using System.Data;
using System.ComponentModel;
using ERP.DAL;
using System.ComponentModel.Design;
using System.Drawing.Design;
using Core.Component;

namespace Components
{
    public class SGVChart: Panel2008, ISGVControl
    {
        Chart chart;
        BaseModule module;
        BaseEntity entity;

        BaseBusinessController Ctrl;

        public ToolStripMenuItem mnuChartType;
        public SGVChart()
            : base()
        {
            chart = new Chart();
            chart.Dock = DockStyle.Fill;
            this.Controls.Add(chart);
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
        public String DataSourceQuery
        {
            set;
            get;
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceTable
        {
            set;
            get;
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String XColumnName
        {
            set;
            get;
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String YColumnName
        {
            set;
            get;
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get;
            set;
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVShortcutString
        {
            set;
            get;
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public void UpdateFormat()
        {
        }

        public void Init()
        {
            

            ChartArea area = new ChartArea("ChartArea");
            chart.ChartAreas.Add(area);


            Legend legend = new Legend("ChartLegend");
            chart.Legends.Add(legend);


            if (String.IsNullOrEmpty(DataSourceTable) == false)
                Ctrl = Host.GetCtrl(DataSourceTable);

            InitContextMenu();
            InitDataSource();
        }


        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            chart.Text = this.Text;
        }

        public virtual void InitDataSource()
        {
            String[] lstYColumnNames = YColumnName.Split('|');
            for (int i = 0; i < lstYColumnNames.Length; i++)
            {
                Series series = new Series(String.Format("Series_{0}", lstYColumnNames[i]));
                chart.Series.Add(series);
                series.XValueMember = XColumnName;
                series.YValueMembers = YColumnName;
            }

            try
            {

                DataSet ds = new DataSet();
                if (String.IsNullOrEmpty(DataSourceQuery) == false)
                    ds = Ctrl.GetDataset(DataSourceQuery);
                else
                    ds = Ctrl.GetAllObjects();

                if (ds != null && ds.Tables.Count > 0)
                    chart.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
            }

        }

        public void InitContextMenu()
        {
            this.ContextMenuStrip = new ContextMenuStrip();
            ToolStripMenuItem mnuChartType = new ToolStripMenuItem();
            mnuChartType.Text = "ChartType";
            this.ContextMenuStrip.Items.Add(mnuChartType);

            Array arrs = Enum.GetValues(typeof(SeriesChartType));
            foreach (var chartType in arrs)
            {
                ToolStripButton mnuItem = new ToolStripButton();
                mnuItem.Text = chartType.ToString();
                mnuItem.Click += new EventHandler(mnuItem_Click);
                mnuChartType.DropDownItems.Add(mnuItem);
            }

        }

        void mnuItem_Click(object sender, EventArgs e)
        {
            ToolStripButton mnuItem = (ToolStripButton)sender;

            SeriesChartType chartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), mnuItem.Text);

            foreach (Series serie in chart.Series)
            {
                serie.ChartType = chartType;
            }

            ToolStripMenuItem parent = (ToolStripMenuItem)mnuItem.OwnerItem;

            foreach (ToolStripButton item in parent.DropDownItems)
            {
                item.Checked = false;
            }

            mnuItem.Checked = true;
        }

        public Core.Component.UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            SGVControlHelper.CreateUIComponentFromControl(component, this);

            if (component != null)
            {
                component.Value = this.Text;
            }
            return component;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }
    }
}
